package com.example.worldcupserver.controller;

import com.example.worldcupserver.bean.User;
import com.example.worldcupserver.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@RestController

public class UserController {
    @Autowired   /*让系统创建数据访问对象*/
               UserDao userDao;
    Map<String,Object> map=new HashMap<>();
    @RequestMapping("/WorldCupApp/user/regist")
    @ResponseBody /*表示返回的是json类型*/
    public Map<String,Object> register(@RequestBody User user){
        //@RequestBody 请求参数为json格式字符串数据

        Map<String,Object>map=null;
        User savedUser = userDao.save(user);//保存到数据库中
        if(savedUser!=null){
            //注册成功
            map =new HashMap<>();
            map.put("success",true);
            map.put("msg","成功");
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }
    @RequestMapping("/WorldCupApp/user/login ")
    @ResponseBody
    public Map<String,Object> login(User user){
        User userFound = userDao.findOne(Example.of(user)).orElse(null);
        Map<String,Object> map=null;
        if(userFound!=null){
            map=new HashMap<>();
            map.put("success",true);
            map.put("msg","成功啊哈哈");
        }else {
            map=new HashMap<>();
            map.put("success",false);
            map.put("msg","失败噜噜噜");
        }
        return map;
    }
    }

